<?php

/**
 * Created by PhpStorm.
 * User: yuchong
 * Date: 2015/2/4
 * Time: 9:53
 */

class Admin_model extends MY_Model {

    public $username;
    public $userid;
    public $m;
    public $password;
    public $last_password;

    public function __construct() {
        parent::__construct();
        $this->_table = "db_admin";
    }

    /**
     *   后台添加新用户
     */
    public function add($data) {
        if(!$this->validate( $data )) {
            return FALSE;
        }
        $data['salt']     = mt_rand( 100000, 999999 );
        $data['password'] = self::make_password( $data['password'], $data['salt'] );
        $userid = $this->insert( $data );
        return $userid ? $userid : false;
    }

    /**
     *   通过后台编辑用户
     */
    public function edit($userid, $data) {
        return $this->update( $data, array("userid" => $userid ) );
    }

    /**
     *   后台普通用户修改密码
     */
    public function password($userid, $password, $last_password, $isMd5 = FALSE) {
        $r = $this->get_by_field( "userid", $userid, "username,password,email,salt" );
        if(!$r) {
            $this->error = "用户不存在";
            return FALSE;
        }
        if(!$this->check_password( $r['password'], $last_password, $r['salt'], $isMd5 )) {
            $this->error = "原密码不正确";
            return FALSE;
        }
        $this->userid = $r['userid'];
        $this->username = $r['username'];
        $this->password = $password;
        $this->last_password = $last_password;

        return $this->set_field( "password", self::make_password( $password, $r['salt'] ), array("userid"=>$userid) );
    }

    /**
     *   后台强制修改密码
     */
    public function force_password($userid, $password) {
        $r = $this->get_by_field("userid",  $userid );
        $this->userid = $r['userid'];
        $this->username = $r['username'];
        $this->password = $password;
        return $this->set_field( "password", self::make_password( $password, $r['salt']), array("userid" => $userid ) );
    }


    /**
     *   删除用户
     */
    public function del($userid) {
        if( $userid) {
            return $this->delete(array('userid', $userid));
        }
        return false;
    }

    /**
     *   字段验证
     */
    public function validate($data) {
        if($this->exists( "username", $data['username'] )) {
            return FALSE;
        }
        if($this->exists( "email", $data['email'] )) {
            return FALSE;
        }
        return TRUE;
    }


    /**
     *   登录操作
     * 1:获取用户信息，检查用户是否存在
     * 2:检查登录记录，判断是否撞库
     * 3:密码检查
     * 4:更新登录信息
     * 5:更新登录次数
     * 6:设置cookie
     * 7:添加登录记录
     */
    public function login($username, $password, $cookietime = 36000) {

        $m = $this->db->from($this->_table)->where(array('username'=>$username,'disabled'=>0))->get()->row_array();
        if(!$m) {
            $this->error = "{$username} 不存在2";
            return FALSE;
        }

        if(!$this->check_password( $m['password'], $password, $m['salt'] )) {
            $this->error = "密码错误";
            return FALSE;
        }

        $this->update( array("lastloginip" => IP, "lastlogintime" => TIME ), array("userid="=>$m['userid']) );
        $this->set_inc( "logintimes", array('userid', $m['userid']) );
        $cookietime += time();
        //setcookie( "admin_auth", str_encode( $m['userid'] . "\t" . $m['username'] . "\t" . $m['roleid'] . "\t" . $m['departmentid'], get_config_item('authkey') ), $cookietime, '/' );
        setcookie( "userid", $m['userid'], $cookietime, '/' );
        setcookie( "username", $m['username'], $cookietime, '/' );
        setcookie( "nickname", $m['nickname'], $cookietime, '/' );

        return true;
    }

    /**
     *   退出操作
     */
    public function logout() {
        $cookietime = time() - 3600;
        setcookie( "auth", '', $cookietime, '/' );
        setcookie( "userid", '', $cookietime, '/' );
        setcookie( "username", '', $cookietime, '/' );
    }


    /**
     *   生成密码
     */
    public static function make_password($password, $salt='') {
        return md5( $password . $salt );
    }


    /**
     *   密码检查
     */
    private function check_password($password, $inputpwd, $salt = "", $isMd5 = FALSE) {
        $md5pass = $isMd5 ? md5( $inputpwd ) : $inputpwd;
        if($password == $md5pass) {
            return TRUE;
        }
        if($password == md5( $md5pass . $salt )) {
            return TRUE;
        }
        return FALSE;
    }

    /**
     * auto:xg
     * 获取全部编辑名
     */
    public function get_all_admin(){
        $res = $this->db->get('db_admin')->result_array();
        return $res;
    }

    /**
     * 获取后台账号个人资料
     */
    function getUserinfo($username){
        $info = $this->db->get_where($this->_table, array('username' => $username))->row_array();
        return $info;
    }
    /**
     * auto:xg
     * 检查账号密码是否正确
     */
    public function check_info($username, $password) {
        $m = $this->db->from($this->_table)->where('username', $username)->get()->row_array();

        if(!$this->check_password( $m['password'], $password, $m['salt'] )) {
            return 1;exit;
        }else{
            return 2;exit;
        }
    }

    /**
     * auto：xg
     * 修改密码
     */
    //e10adc3949ba59abbe56e057f20f883e
    public function change_pass($username,$password){
        $set_date['password'] = $this->make_password($password);
        $this->db->where('username', $username);
        $this->db->update($this->_table, $set_date);
        //echo $this->db->last_query();exit;
        return $this->db->affected_rows();
    }

    /**
     * auto:xg
     * 添加管理员
     */
    public function save_admin($date){
        $this->db->insert($this->_table, $date);
        return $this->db->affected_rows();
    }

    /**
     * auto:xg
     * 改变编辑状态
     */
    public function chang_disable($flag,$userid){
        $set_date['disabled'] = $flag;
        $this->db->where('userid', $userid);
        $this->db->update($this->_table, $set_date);
        return $this->db->affected_rows();
    }
}